package org.example.service;
import org.example.entity.QuestionInfoEntity;
import org.example.entity.ResultsInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AutoGradeService {
    @Autowired
    private QuestionInfoService questionInfoService;

    @Autowired
    private ResultsInfoService resultsInfoService;

    public float autoGradeExam(int examId, int studentId) {
        float totalScore = 0;

        // 1. 获取当前考试的所有题目
        List<QuestionInfoEntity> questions = questionInfoService.getQuestionsByExamId(examId);
       // List<ResultsInfoEntity> results=ResultServiceImpl.getResultsByESId(examId, studentId);

        if (questions == null || questions.isEmpty()) {
            throw new RuntimeException("考试题目为空！");
        }
        for (QuestionInfoEntity question : questions) {
            //2.查找对应的正确答案答案
            String correctAnswer = question.getContentAnswer();
            long quesId=question.getQuestionId();
            //3.获取学生答案
            ResultsInfoEntity result = resultsInfoService.getResultsByQuestionId(quesId, studentId);
            if (result == null) {
                continue; // 如果学生未作答，跳过该题
            }

            String studentAnswer = result.getStuAnswer();

            // 4. 判断学生答案是否正确
            if (studentAnswer.equals(correctAnswer)) totalScore += question.getScore(); // 如果答对了，累加分数
        }
        return totalScore;
    }
}
